package com.jxdinfo.hussar.example.common.schedulingtask.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxdinfo.hussar.example.common.schedulingtask.dao.MdClientMapper;
import com.jxdinfo.hussar.example.common.schedulingtask.dao.MdWorkshopMapper;
import com.jxdinfo.hussar.example.common.schedulingtask.model.MdUnitMeasure;
import com.jxdinfo.hussar.example.common.schedulingtask.model.MdWorkshop;
import com.jxdinfo.hussar.example.common.schedulingtask.service.MdClientService;
import com.jxdinfo.hussar.example.common.schedulingtask.service.MdWorkshopService;
import com.jxdinfo.hussar.example.common.schedulingtask.vo.MdUnitMeasureVO;
import com.jxdinfo.hussar.example.common.schedulingtask.vo.MdWorkshopVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 车间表 服务实现类
 * </p>
 *
 * @author 乔镔
 * @since 2024-04-08
 */
@Service("com.jxdinfo.hussar.example.common.schedulingtask.service.impl.MdWorkshopServiceImpl")
public class MdWorkshopServiceImpl extends ServiceImpl<MdWorkshopMapper, MdWorkshop> implements MdWorkshopService {

    private static final Logger logger = LoggerFactory.getLogger(MdWorkshopServiceImpl.class);


    @Autowired
    private MdWorkshopService mdWorkshopService;

    @Autowired
    private MdWorkshopMapper mdWorkshopMapper;
    @Override
    public void manualSynchronization() {
        logger.info("\n手动同步-车间表-开始同步车间表");
        LocalDateTime nowTime = LocalDateTime.now();
        // 1查询erp所有的车间表信息
        List<MdWorkshopVO> allMdWorkshops = mdWorkshopMapper.selectAllClient();
        // 2车间表信息转换成主键-对象map集合
        Map<Long, MdWorkshopVO> allMdWorkshopMap = allMdWorkshops.stream().collect(Collectors.toMap(MdWorkshopVO::getWorkshopId, t -> t));
        // 3车间表信息中的id转换成id列表
        List<Long> erpAllMdWorkshopIdList = allMdWorkshops.stream().map(MdWorkshopVO::getWorkshopId).collect(Collectors.toList());
        // 4查询所有的mes车间表信息
        QueryWrapper<MdWorkshop> mdWorkshopWrapper = new QueryWrapper<>();
        mdWorkshopWrapper.eq("deleted_flag","0");
        List<MdWorkshop> mdWorkshops = mdWorkshopMapper.selectList(mdWorkshopWrapper);
        // 4车间表信息转换成主键-对象map集合
        Map<Long, MdWorkshop> allMesMdWorkshopMap = mdWorkshops.stream().collect(Collectors.toMap(MdWorkshop::getWorkshopId, t -> t));
        // 5mes中的客户信息转换成id列表,
        List<Long> mesIdList = mdWorkshops.stream().map(MdWorkshop::getWorkshopId).collect(Collectors.toList());
        // 6找出erp中有但是mes中没有的id，这写id需要插入
        ArrayList<Long> erpHasMesNoIdList = new ArrayList<>();
        for (int i = 0; i < erpAllMdWorkshopIdList.size(); i++) {
            if (!mesIdList.contains(erpAllMdWorkshopIdList.get(i))) {
                erpHasMesNoIdList.add(erpAllMdWorkshopIdList.get(i));
            }
        }
        if (erpHasMesNoIdList.size() > 0) {
            // 循环创建对象，保存待要插入的结果
            ArrayList<MdWorkshop> insertList = new ArrayList<>();
            for (Long insertId : erpHasMesNoIdList) {
                MdWorkshopVO mdWorkshopVO = allMdWorkshopMap.get(insertId);
                MdWorkshop mdWorkshop = new MdWorkshop();
                mdWorkshop.setWorkshopCode(mdWorkshopVO.getWorkshopCode());  // 车间编码
                mdWorkshop.setWorkshopName(mdWorkshopVO.getWorkshopName());   // 车间名称
                mdWorkshop.setWorkshopId(mdWorkshopVO.getWorkshopId());   // 主键
                mdWorkshop.setEnableFlag(mdWorkshopVO.getEnableFlag());   // 是否启用
                mdWorkshop.setErpWorkshopId(mdWorkshopVO.getWorkshopId()); //erp主键
                mdWorkshop.setCreateBy(mdWorkshopVO.getCreateBy());
                mdWorkshop.setCreateTime(mdWorkshopVO.getCreateTime());
                mdWorkshop.setUpdateBy(mdWorkshopVO.getUpdateBy());
                mdWorkshop.setUpdateTime(mdWorkshopVO.getUpdateTime());
                mdWorkshop.setScheduleTime(nowTime);
                insertList.add(mdWorkshop);
            }
            mdWorkshopService.saveBatch(insertList);
        }
        // 7找出erp和mes中都有的，这些需要更新
        ArrayList<MdWorkshop> mesMdWorkshopUpdateList = new ArrayList<>();
        for (int i = 0; i < mesIdList.size(); i++) {
            if (erpAllMdWorkshopIdList.contains(mesIdList.get(i))){
                mesMdWorkshopUpdateList.add(allMesMdWorkshopMap.get(mesIdList.get(i)));
            }
        }
        if (mesMdWorkshopUpdateList.size()>0) {
            ArrayList<MdWorkshop> mdWorkshopUpdateList = new ArrayList<>();
            for (MdWorkshop mdWorkshop : mesMdWorkshopUpdateList) {
                Long erpWorkShopId = mdWorkshop.getWorkshopId();
                MdWorkshopVO mdWorkshopVO = allMdWorkshopMap.get(erpWorkShopId);
                mdWorkshop.setWorkshopCode(mdWorkshopVO.getWorkshopCode());  // 料
                mdWorkshop.setWorkshopName(mdWorkshopVO.getWorkshopName());   //
                mdWorkshop.setEnableFlag(mdWorkshopVO.getEnableFlag());   //
                mdWorkshop.setScheduleTime(nowTime);
                mdWorkshop.setCreateBy(mdWorkshopVO.getCreateBy());
                mdWorkshop.setCreateTime(mdWorkshopVO.getCreateTime());
                mdWorkshop.setUpdateBy(mdWorkshopVO.getUpdateBy());
                mdWorkshop.setUpdateTime(mdWorkshopVO.getUpdateTime());
                mdWorkshopUpdateList.add(mdWorkshop);
            }
            mdWorkshopService.updateBatchById(mdWorkshopUpdateList);
        }
    }
}
